First, we start by historic election data, going back 4-5
elections.
party <- c("SPD", "CDU/CSU", "Greens", "FDP", "AfD", "Left")
percentage <- c(25.7, 24.1, 14.7, 11.4, 10.4, 4.9)
# Combine the vectors into a data.table
election_data_2021 <- data.table(Party = party, Percentage = percentage)
party_colors <- c("SPD" = "#E3000F", "CDU/CSU" = "#000000", "Greens" = "#64A12D", "FDP" = "#FFD100", "AfD" = "#009EE0", "Left" = "#E60012")
election_data_2021 <- election_data_2021[order( -Percentage)]
election_data_2021$Party <- factor(election_data_2021$Party, levels = election_data_2021$Party)
# Create the plot
p <- plot_ly(
data = election_data_2021,
x = ~Party,
y = ~Percentage,
type = 'bar',
marker = list(color = ~party_colors[Party]) # Apply color based on party
) %>%
layout(
title = "Election Results",
xaxis = list(title = "Party"),
yaxis = list(title = "Percentage of Votes")
)
# View the data table
p